<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/11/27
 * Time: 8:37
 */

namespace App\Models;
use App\Models\BaseModel;
/**
 * Created by 宋帅的自动工具
 * 生成时间:2019-11-27 08:37:14
 */
/**
This is the model class for table "hezuoguanxi".
 * @property int10unsigned id
 * @property int10unsigned my	表示我的id
 * @property int10unsigned friend	朋友的id
 * @property varchar255 my_friend	我和朋友的id,按照从小到大的排序,用_排序拼接
 */
class HezuoguanxiModel extends Model{
    // 当前模型名称
    protected $table = 'hezuoguanxi';
    public function attributeLabels() {
        return [
            'id' => '',
            'my' => '表示我的id',
            'friend' => '朋友的id',
            'my_friend' => '我和朋友的id,按照从小到大的排序,用_排序拼接',
        ];
    }
    /**
     * 拼接两个用户id
    */
    public static function pinjie($u1,$u2){
        $pinjie=$u1.'_'.$u2;
        if((int)$u1>(int)$u2){
            $pinjie=$u2.'_'.$u1;
        }
        return $pinjie;
    }
    /**
     * 判断是否为合作关系
     * $pinjie拼接的用户id
    */
    public static function ishezuo($u1,$u2){
        $pj=self::pinjie($u1,$u2);

        return self::where('my_friend','LIKE',$pj)->exists();
    }

    /**
     * 添加合作关系
    */
    public static function  addGuanxi($u1,$u2){
        $pj=self::pinjie($u1,$u2);
        if(self::ishezuo($u1,$u2)){
            return;
        }
        $new=new self;
        $new->my_friend=$pj;
        $new->my=$u1;
        $new->friend=$u2;
        $new->save();
    }
}